global class BatchtocheckOppstatusEDC implements Database.Batchable<SObject>,Database.Stateful{

    public String querystring;
    global Database.QueryLocator start(Database.BatchableContext BC){
            querystring='select id,CloseDate,Upper_EDC__c,StageName,Reasons_for_lost__c,IsActive__c from Opportunity';
            return Database.getQueryLocator(querystring);
        }
    global void execute(Database.BatchableContext BC, List<sObject> sobjectlist){
           List<Opportunity> opportunityupdatelist=new List<Opportunity>();
       
            for(sObject sobj: sobjectlist){         
                Opportunity opp=(Opportunity)sobj;
                integer differenceindays=(system.today()).daysBetween(opp.closedate); 
                                   
                system.debug('@@'+opp.Upper_EDC__c);
                system.debug('##'+system.Today());
                system.debug('differenceindays'+differenceindays);
                
                if(differenceindays<0&&opp.IsActive__c==true){
                    opp.Is_Batch__c=true;
                    if(opp.StageName!='Closed Won'){
                        opp.StageName='Closed Lost';
                        opp.Reasons_for_lost__c='EDC Expired';
                        opp.IsActive__c=False;
                    }else{                   
                         opp.IsActive__c=False;
                         opp.Reasons_for_lost__c='';
                    }
                    opportunityupdatelist.add(opp);
                }
            }
            update opportunityupdatelist;
       }
       
    global void finish(Database.BatchableContext BC){
        
       }
}